package iosr11.ui.data;

import iosr11.Service.ServiceLocatorImpl;
import iosr11.Service.ProjectSubjectService;
import iosr11.Service.ProjectGroupService;
import iosr11.data.ProjectSubject;
import iosr11.data.ProjectGroup;
import iosr11.ui.common.CurrentUserBean;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;


public class ProjectListBean {
	private Logger logger = Logger.getLogger("iosr.ui.data");
    private List<ProjectSubject> projectList;
	private ProjectSubjectService service;
	private ProjectGroupService service_group;
	private HtmlDataTable dataTable;
	private CurrentUserBean currentUser; 
    
    
	public ProjectListBean(){
		this.service = (ProjectSubjectService)ServiceLocatorImpl.getInstance().getService(ProjectSubject.class);
		//this.ensureListNotNull();
		if(this.service==null){
			logger.severe("Could not obtain reference to ProjectSubjectService");
			return;
		}
	}

	public List<ProjectSubject> getAllProjects(){
		//ensureListNotNull();
		if(this.service != null){
			this.projectList = service.getAll();
		}
		if(this.projectList == null){
			this.projectList = new ArrayList<ProjectSubject>();
		}
		return this.projectList;
	}
	
	
	public HtmlDataTable getDataTable(){
		return this.dataTable;
	}
	public void setDataTable(HtmlDataTable table){
		this.dataTable = table;
	}
	/*
	private void saveSelectedStudentId(){
		 Student selectedStudent = (Student) dataTable.getRowData();
		 FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(
				 SessionMapKeys.SelectedStudentEmail, selectedStudent.getEmail());
	}*/
	
	public String registerProject(){
		ProjectSubject selectedSubject = (ProjectSubject) dataTable.getRowData();
		this.service_group = (ProjectGroupService)ServiceLocatorImpl.getInstance().getService(ProjectGroup.class);
		/*if(this.service_group==null){
			logger.severe("Could not obtain reference to ProjectGroupService");
			throw new Exception("Could not obtain reference to ProjectGroupService");
		}*/
		if (service_group.setSubject(this.currentUser.getStudent().getProjectGrp(), selectedSubject))
			return "true";
		else {
			logger.severe("Could not register to ProjectSubject");
			return "false";
		}
	} 
	/*
	public String editStudentMarks(){
		 saveSelectedStudentId();
		 return "editStudentMarks";
	} 
	
	public String editStudentDetails(){
		 saveSelectedStudentId();
		 return "editStudent";
	} 

	public String addStudentDetails(){
		 FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(SessionMapKeys.SelectedStudentEmail);
		 return "editStudent";
	} 
	 */
	
	public ProjectSubject getSubject(){
		//ProjectSubject subject;
		if(this.service==null){
			this.service = (ProjectSubjectService)ServiceLocatorImpl.getInstance().getService(ProjectSubject.class);
		}
		if(this.service==null){
			logger.severe("Could not obtain reference to ProjectSubjectService");
			return null;
		}
		return service.getSubject(this.currentUser.getStudent().getProjectGrp());
	} 
	
	public CurrentUserBean getCurrentUser() {
		return currentUser;
	}
	
	public void setCurrentUser(CurrentUserBean currentUser) {
		this.currentUser = currentUser;
	}
}


